package work.lianbiao;

import work.ListNode;

import java.util.ArrayList;
import java.util.List;

public class Problem234 {
    //2.22
    public boolean isPalindrome(ListNode head) {
        List<Integer> list = new ArrayList<Integer>();
        ListNode cur = head;
        while (cur != null) {
            list.add(cur.val);
            cur = cur.next;
        }
        int a = 0;
        int b = list.size() - 1;
        while (a < b) {
            if (!(list.get(a) == list.get(b))) {
                return false;
            }
            a++;
            b--;
        }
        return true;
    }


    public static void main(String[] args) {
        int[] a = {1, 2, 3, 2, 1};
        ListNode listNode = ListNode.listNodeFromArray(a);
        Problem234 problem234 = new Problem234();
        boolean palindrome = problem234.isPalindrome(listNode);
        System.out.println(palindrome);
    }

    public boolean isPalindrome1(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        if (fast != null) {
            slow = slow.next;
        }
        ListNode rev = Problem206.reverseList(slow);
        while (rev != null) {
            //然后比较，判断节点值是否相等
            if (fast.val != rev.val)
                return false;
            fast = fast.next;
            rev = rev.next;
        }
        return true;
    }



}
